import React, {useEffect, useState} from 'react';
import {Table} from 'antd';
import {connect} from 'umi';
import {resultSum} from '@/utils/utils';

const AllocationTable = (props) => {

  const {scaleInfo,enterNum,tempRecord} = props;
  const [data,setData] = useState([
    {name:'建议分配（小数点一位）',percentageA:0,percentageBPlus:0,percentageB:0,percentageC:0,percentageD:0},
    {name:'实际分配（整数）',percentageA:0,percentageBPlus:0,percentageB:0,percentageC:0,percentageD:0}
  ]);
  const [countNumber,setCountNumber] = useState(0);
  const title = () => { return `分配数量：` + countNumber + `/` + enterNum};
  const columns = [
    {
      title: '',
      width:'20%',
      dataIndex: 'name',
    },
    {
      title: `A(`+ (Object.keys(scaleInfo).length && scaleInfo.percentageA) +`%)`,
      align: 'center',
      dataIndex: 'percentageA',
    },
    {
      title: `B+(`+ (Object.keys(scaleInfo).length && scaleInfo.percentageBPlus) +`%)`,
      align: 'center',
      dataIndex: 'percentageBPlus',
    },
    {
      title: `B(`+ (Object.keys(scaleInfo).length && scaleInfo.percentageB) +`%)`,
      align: 'center',
      dataIndex: 'percentageB',
    },
    {
      title: `C(`+ (Object.keys(scaleInfo).length && scaleInfo.percentageC) +`%)`,
      align: 'center',
      dataIndex: 'percentageC',
    },
    {
      title: `D(`+ (Object.keys(scaleInfo).length && scaleInfo.percentageD) +`%)`,
      align: 'center',
      dataIndex: 'percentageD',
    },
  ];


  useEffect(() => {
    const newData = [...data];
    let temp = newData[0];
    Object.keys(scaleInfo).forEach(item => {
      Object.keys(temp).forEach(it => {
        if(item === it && it !== 'name'){
          temp[it] = ((scaleInfo[item] * 0.01) * enterNum).toFixed(1);
        }
      });
    });
    setData(newData);
  }, [enterNum]);

  useEffect(() => {
    if(Object.keys(tempRecord).length > 0){
      setCountNumber(resultSum(tempRecord));
      const newData = [...data];
      let temp = newData[1];
      Object.keys(tempRecord).forEach(item => {
        Object.keys(temp).forEach(it => {
          if(item === it && it !== 'name'){
            temp[it] = tempRecord[item];
          }
        });
      });
      setData(newData);
    }
  }, [tempRecord]);

  return (
    <Table
      title={title}
      rowKey='name'
      dataSource={data}
      columns={columns}
      pagination={false}
    />
  )
};

export default connect(() => ({}))(AllocationTable);
